<!DOCTYPE html><h
tml>
    <head>
        <title>Public Registry Settings</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
        <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
        <script type="text/javascript" src="../../script/jquery.min.js"></script>
        <script type="text/javascript" src="../../script/semantic/semantic.min.js"></script>
        <!-- <script type="text/javascript" src="../../script/ao_module.js"></script> -->
    </head>
    <body>
        <div class="ui container">
            <div class="ui basic segment">
                <div class="ui header">
                    <i class="key icon"></i>
                        <div class="content">
                        Public Registry
                        <div class="sub header">Allow public to register an account on this system</div>
                    </div>
                </div>
            </div>
            <div id="succdialog" style="display:none;" class="ui green inverted segment"><i class="checkmark icon"></i> Registry Setting Updated</div>
            <div id="errdialog" style="display:none;" class="ui red inverted segment">Update Failed: <span id="errmsg"></span></div>
            <form class="ui form" onsubmit="handleFormSubmit(event);">
                <div class="ui segment">
                    <div class="field">
                        <div class="ui toggle checkbox">
                            <input type="checkbox" id="enableReg" tabindex="0" class="hidden">
                            <label>Enable Public Account Registry</label>
                        </div>
                    </div>
                </div>
                <div class="field">
                    <label>Default Permission Group for New User</label>
                    <div class="ui fluid search selection dropdown">
                            <input type="hidden" name="pg" id="pg">
                            <i class="dropdown icon"></i>
                            <div class="default text">Select Permission Group</div>
                            <div id="pglist" class="menu">
                                    
                            </div>
                    </div>
                </div>
                <div class="ui blue button" tabindex="0" onclick="handleFormSubmit(event);">Update</div>
            </form>
            <div class="ui divider"></div>
            <div class="ui basic segment">
                <h4>Registered User List</h4>
                <a class="ui green right floated button" href="../../system/register/listUserEmails?csv=true" target="_blank">Download as CSV</a>
                <p>A list of registered users. Be noted that the user registration information will not be deleted when a user account is deregistered. The status of the user account status will be shown in the "Still Registered" collum.</p>
            </div>
            <table class="ui celled table">
                <thead>
                    <tr><th>Username</th>
                    <th>Email</th>
                    <th>Still Registered</th>
                </tr></thead>
                <tbody id="userlist">
                    
                </tbody>
                </table>
                <br>
                <button class="ui blue button" onclick="initUserRegisterInfoList();"><i class="refresh icon"></i>Refresh Table</button>
                <button class="ui right floated red button" onclick="clearUnregisteredUserInfo();"><i class="remove icon"></i> Clear Unregistered Users Information</button>
                
                <br><br><br>
        
        </div>
        <script>
            $(".ui.checkbox").checkbox();

            initRegStatus();
            function initRegStatus(){
                $.get("../../system/register/getAllowRegistry", function(data){
                    if (data == true){
                        $("#enableReg").parent().checkbox("check");
                    }
                });
                $.get("../../system/permission/listgroup", function(data){
                    if (data.error !== undefined){
                        alert(data.error);
                    }else{
                        $("#pglist").html("");
                        data.forEach(gp => {
                            $("#pglist").append(`<div class="item" data-value="${gp}">${gp}</div>`);
                        })
                    }
                    $(".ui.dropdown").dropdown();

                    //Get the current default group
                    $.get("../../system/register/setDefaultGroup?get=true", function(data){
                        $("#pglist").parent().dropdown("set selected",data);
                    });
                });

            }

            function handleFormSubmit(e){
                e.preventDefault();
                var pg = $("#pg").val();
                var regEnabled = $("#enableReg")[0].checked;
                
                $.ajax({
                    url: "../../system/register/setDefaultGroup",
                    data: {"defaultGroup": pg},
                    success: function(data){
                        if (data.error !== undefined){
                            showErrorMessage(data.error);
                        }else{
                            showSuccessMessage();
                        }
                    },
                    error: function(){
                        showErrorMessage("Server Communication Error");
                    }
                });

                $.ajax({
                    url: "../../system/register/setAllowRegistry",
                    data: {"allow": regEnabled},
                    success: function(data){
                        if (data.error !== undefined){
                            showErrorMessage(data.error);
                        }else{
                            showSuccessMessage();
                        }
                    },
                    error: function(){
                        showErrorMessage("Server Communication Error");
                    }
                });
            }

            function showSuccessMessage(){
                $("#succdialog").stop().finish().slideDown('fast').delay(3000).slideUp('fast');
            }

            function showErrorMessage(err){
                $("#errmsg").text(err);
                $("#errdialog").stop().finish().slideDown('fast').delay(3000).slideUp('fast');

            }

            initUserRegisterInfoList();
            function initUserRegisterInfoList(){
                $("#userlist").html("");
                $.get("../../system/register/listUserEmails", function(data){
                    if (data.error !== undefined){
                        alert(data.error)
                    }else{
                        data.forEach(userinfo => {
                            var icon = "green checkmark icon";
                            if (userinfo[2] == false){
                                icon = "red remove icon"
                            }
                            $("#userlist").append(`<tr>
                                <td>${userinfo[0]}</td>
                                <td><a href="mailto:${userinfo[1]}">${userinfo[1]}</a></td>
                                <td><i class="${icon}"></i></td>
                            </tr>`);
                        });

                        if (data.length == 0){
                            $("#userlist").append(`<tr><td col="3"><i class="remove icon"></i> There is no user register record in the database.</td></tr>`);
                        }
                    }
                })
            }

            //Remove all unregistered user information
            function clearUnregisteredUserInfo(){
                if (confirm("Confirm Removing ALL UNREGISTERED USERS INFORMATION?")){
                    $.get("../../system/register/cleanUserRegisterInfo", function(data){
                        initUserRegisterInfoList();
                    });
                }
            }
        </script>
    </body>
</html>